package com.db;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.xutils.DbManager;
import org.xutils.x;
import org.xutils.db.sqlite.SqlInfo;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.db.table.DbModel;
import org.xutils.db.table.TableEntity;

import android.net.http.SslCertificate.DName;

import com.model.RWDataModel;
import com.model.ZWDataModel;
import com.utils.Const;

public class DBService {

	public static DbManager getDB(){
		DbManager.DaoConfig daoConfig=new DbManager.DaoConfig()
        .setDbName(Const.DB_NAME)
        .setDbVersion(Const.DB_VERSION)
        .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
            @Override
            public void onUpgrade(DbManager db, int oldVersion, int newVersion) {

            }
        })
        .setDbOpenListener(new DbManager.DbOpenListener() {
            @Override
            public void onDbOpened(DbManager db) {
                db.getDatabase().enableWriteAheadLogging();
            }
        })
        //.setDbDir(new File("/sdcard/download/"))
        .setAllowTransaction(true)
        .setTableCreateListener(new DbManager.TableCreateListener() {
			
			@Override
			public void onTableCreated(DbManager arg0, TableEntity<?> arg1) {
				// TODO Auto-generated method stub
				
			}
		});

		DbManager db=x.getDb(daoConfig);
		return db;
	}
	/**
	 * 获取用户创建文件夹
	 * @param type 功能类型
	 * @param userId 用户id
	 * @return
	 */
	public static List<ZWDataModel> getFromModel(int type,String userId){
		List<ZWDataModel> data = new ArrayList<ZWDataModel>();
		try {
			
			DbManager db = getDB();
			List<DbModel> dbms = db.findDbModelAll(new SqlInfo("select * from from_db where type='" + type+"' and userId='" + userId+ "'"));
			for (DbModel d : dbms) {
				ZWDataModel f = new ZWDataModel();
				f.id = d.getInt("id");

				
				data.add(f);
			}
			db.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}
	/**
	 * 获取文件夹内容
	 * @param formId
	 * @return
	 */
	public static List<RWDataModel> getFromDataModel(String formId){
		List<RWDataModel> data = new ArrayList<RWDataModel>();
		try {
			
			DbManager db = getDB();
			List<DbModel> dbms = db.findDbModelAll(new SqlInfo("select * from from_data_db where formId='" + formId+"'"));
			for (DbModel d : dbms) {
				RWDataModel f = new RWDataModel();
				f.id = d.getInt("id");
				
				data.add(f);
			}
			db.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}
	/**
	 * 删除文件
	 * @param date
	 * @return
	 */
	public static boolean deleteFormData(long date){
		boolean flag =  false;
		try {
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * 清理服务
	 */
	public static void clearService(){
		DbManager db = getDB();
		
		try {
			List<DbModel> dbms = db.findDbModelAll(new SqlInfo("select * from from_db where date<'" + (new Date().getTime() - Const.CLEAR_TIME) + "'"));

			for (DbModel dm : dbms) {
				List<DbModel> dbs = db.findDbModelAll(new SqlInfo("select * from from_data_db where formId='" + dm.getInt("id")+"'"));
				for (DbModel d : dbs) {					
					deleteFile(d.getString("bdPath"));
				}
				
				db.delete(RWDataModel.class, WhereBuilder.b("formId", "=", dm.getInt("id")));
				db.delete(ZWDataModel.class, WhereBuilder.b("id", "=", dm.getInt("id")));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 删除文件
	 * @param path
	 */
	private static void deleteFile(String path){
		try {
			File file = new File(path);
			if (file.exists()) {
				file.delete();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
